﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using ComicIsland.Common;
using ComicIsland.Common.Entities.InterfaceJson;
using ComicIsland.Common.WebHelper;
using Microsoft.Phone.Controls;

/**
 * 创建时间：2014/11/27 14:17:14
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace ComicIsland.App
{
    public class TestInterface
    {
        /// <summary>
        ///  获取书城焦点图
        ///  <para>adgroupid:1.书城，2 书架，3, 社区推荐墙,4 IOS书城,5 IOS书架推广,6 Ipad书城焦点图,7 书城焦点图2,8 社区广告,9 布卡</para>
        ///  <para>channelid:</para>
        ///  <para>channelid:</para>
        ///  <para>platformtype:</para>
        /// </summary>
        public void GetBookStoreFocusImage(PhoneApplicationPage page)
        {

            string requestUrl = "http://58.215.139.194/comic/getproad?adgroupid=1&channelid=1&platformtype=1";


            WebTaskHelper webTask = new WebTaskHelper(new Uri(requestUrl));
            IAsyncResult ia = webTask.BeginWebTask(delegate(IAsyncResult callBackResult)
            {
                try
                {
                    page.Dispatcher.BeginInvoke(() =>
                    {
                        #region 获取结果处理
                        string result = webTask.EndWebTask(callBackResult);

                        if (string.IsNullOrWhiteSpace(result))
                        {
                            LogHelper.LogError("Result is empty.");
                            //MessageToastManager.Show("未获取到数据！", MessageToastLevel.Error);
                            return;
                        }
                        //
                        if (result.StartsWith("exception:"))
                        {
                            LogHelper.LogError(result);
                            //MessageToastManager.Show("网络连接失败！", MessageToastLevel.Error);
                            return;
                        }
                        LogHelper.LogInfo(result, this.GetType(), MethodBase.GetCurrentMethod());
                        //
                        JsonBookStoreFocusBase rootCharacterString = JSONHelper.Deserialize<JsonBookStoreFocusBase>(result);
                        if (!(rootCharacterString != null && !string.IsNullOrWhiteSpace(rootCharacterString.Info.JsonString)))
                        {
                            //MessageToastManager.Show("返回结果为空！", MessageToastLevel.Error);
                        }
                        else
                        {
                            var items = JSONHelper.Deserialize<ObservableCollection<JsonBookStoreFocusAdlistjson>>(rootCharacterString.Info.JsonString);
                        }

                        #endregion 获取结果处理
                    });
                }
                catch (Exception err)
                {

                }
            }, webTask);
        }

        /// <summary>
        /// 获取书城各列表数据
        /// </summary>
        /// <param name="page"></param>
        public void GetBookStoreAlbum(PhoneApplicationPage page)
        {
            string requestUrl = "http://58.215.139.194/comic/getalbumlist?channelid=1&platformtype=1";


            WebTaskHelper webTask = new WebTaskHelper(new Uri(requestUrl));
            IAsyncResult ia = webTask.BeginWebTask(delegate(IAsyncResult callBackResult)
            {
                try
                {
                    page.Dispatcher.BeginInvoke(() =>
                    {
                        #region 获取结果处理
                        string result = webTask.EndWebTask(callBackResult);

                        if (string.IsNullOrWhiteSpace(result))
                        {
                            LogHelper.LogError("Result is empty.");
                            //MessageToastManager.Show("未获取到数据！", MessageToastLevel.Error);
                            return;
                        }
                        //
                        if (result.StartsWith("exception:"))
                        {
                            LogHelper.LogError(result);
                            //MessageToastManager.Show("网络连接失败！", MessageToastLevel.Error);
                            return;
                        }
                        LogHelper.LogInfo(result, this.GetType(), MethodBase.GetCurrentMethod());
                        //
                        var rootCharacterString = JSONHelper.Deserialize<JsonBookStoreAlbumBase>(result);
                        if (!(rootCharacterString != null && rootCharacterString.Info != null && rootCharacterString.Info.Count > 0))
                        {
                            //MessageToastManager.Show("返回结果为空！", MessageToastLevel.Error);
                        }
                        else
                        {
                            foreach (JsonBookStoreAlbumInfo item in rootCharacterString.Info)
                            {
                                var areablocks = JSONHelper.Deserialize<ObservableCollection<JsonBookStoreAlbumInfoAreaBlock>>(item.Bigbooksjson);                                
                            }
                        }

                        #endregion 获取结果处理
                    });
                }
                catch (Exception err)
                {

                }                
            }, webTask);
        }


        public void GetComicDetailBase(PhoneApplicationPage page,int bookId)
        {
            string requestUrl = string.Concat("http://58.215.139.194/comic/comicsdetail_v2?bigbookid=", bookId.ToString ());


            WebTaskHelper webTask = new WebTaskHelper(new Uri(requestUrl));
            IAsyncResult ia = webTask.BeginWebTask(delegate(IAsyncResult callBackResult)
            {
                try
                {
                    page.Dispatcher.BeginInvoke(() =>
                    {
                        #region 获取结果处理
                        string result = webTask.EndWebTask(callBackResult);

                        if (string.IsNullOrWhiteSpace(result))
                        {
                            LogHelper.LogError("Result is empty.");
                            //MessageToastManager.Show("未获取到数据！", MessageToastLevel.Error);
                            return;
                        }
                        //
                        if (result.StartsWith("exception:"))
                        {
                            LogHelper.LogError(result);
                            //MessageToastManager.Show("网络连接失败！", MessageToastLevel.Error);
                            return;
                        }
                        LogHelper.LogInfo(result, this.GetType(), MethodBase.GetCurrentMethod());
                        //
                        var rootCharacterString = JSONHelper.Deserialize<JsonComicDetailBase>(result);
                        if (!(rootCharacterString != null && rootCharacterString.Info != null && rootCharacterString.Info.Comicsdetail != null && rootCharacterString.Info.Comicsdetail.Count > 0))
                        {
                            //MessageToastManager.Show("返回结果为空！", MessageToastLevel.Error);
                        }
                        else
                        {
                            var detailInfo = rootCharacterString.Info.Comicsdetail;
                        }

                        #endregion 获取结果处理
                    });
                }
                catch (Exception err)
                {

                }
            }, webTask);
        }




    }
}
